﻿using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using Zocono.Infrastructure.Common;
using Zocono.WCS.Domain.EntityInfo;
using Zocono.WCS.Infrastructure.ApllicationDto.DtoWcsManage;
using Zocono.WCS.Infrastructure.Repository;

namespace Zocono.WCS.Domain.UserManageDomain
{
    public class UserRoleInfoService : IUserRoleInfoService
    {
        private IUserRoleInfoRepository _UserRoleInfoRepository;
        private IMapper _Mapper;
        private readonly ISnowflake _snowflake;

        public UserRoleInfoService(IUserRoleInfoRepository userRoleInfoRepository, IMapper mapper, ISnowflake snowflake)
        {
            _UserRoleInfoRepository = userRoleInfoRepository;
            _Mapper = mapper;
            _snowflake = snowflake;
        }

        public async Task<List<UserRoleInfoDto>> GetUserRoleInfos()
        {
            List<UserRoleInfoDto> userRoleInfoDtos = new List<UserRoleInfoDto>();
            var result = await _UserRoleInfoRepository.GetListAsync();
            _Mapper.Map(result, userRoleInfoDtos);
            return userRoleInfoDtos;
        }

        public async Task<List<UserRoleInfoDto>> GetUserRoleInfos(long userID)
        {
            List<UserRoleInfoDto> userRoleInfoDtos = new List<UserRoleInfoDto>();
            var result = await _UserRoleInfoRepository.AsSugarClient().Queryable<UserRoleInfo>().Where(x => x.UserID == userID).ToListAsync();
            _Mapper.Map(result, userRoleInfoDtos);
            return userRoleInfoDtos;
        }

        /// <summary>
        /// 添加用户和权限
        /// </summary>
        /// <param name="userMangerDto"></param>
        /// <returns></returns>
        public async Task<bool> InsertUserRole(List<UserMangerDto> userMangerDto)
        {
            UserRoleInfoDto userRoleInfoDto = new UserRoleInfoDto();
            UserRoleInfo userRoleInfo = new UserRoleInfo();

            UserInfo userInfo = new UserInfo();
            try
            {
                _UserRoleInfoRepository.AsTenant().BeginTran();

                var userdto = userMangerDto.Where(x => x.UserName != null).FirstOrDefault();
                _Mapper.Map(userdto, userInfo);
                userInfo.UserID = _snowflake.GetId();
                userInfo.IsDelete = false;
                userInfo.UpdateTime = DateTime.Now;
                await _UserRoleInfoRepository.AsSugarClient().Insertable<UserInfo>(userInfo).ToAsync();


                var userRoledto = userMangerDto.Where(x => x.CreateUserName.IsEmpty() == false).FirstOrDefault();
                userRoleInfoDto.UserID = userInfo.UserID;
                userRoleInfoDto.UpdateTime = DateTime.Now;
                userRoleInfoDto.IsDelete = false;
                userRoleInfoDto.CreateUserName = userRoledto.CreateUserName;
                userMangerDto.Where(x => x.RoleID.IsEmpty() == false).ForEach(async x =>
                {
                    userRoleInfoDto.UserRoleID = _snowflake.GetId();
                    userRoleInfoDto.RoleID = x.RoleID;
                    _Mapper.Map(userRoleInfoDto, userRoleInfo);
                    await _UserRoleInfoRepository.InsertAsync(userRoleInfo);
                });

                _UserRoleInfoRepository.AsTenant().CommitTran();
                return true;

            }
            catch (Exception ex)
            {
                _UserRoleInfoRepository.AsTenant().RollbackTran();
                throw new Exception(ex.Message);
            }

        }

        /// <summary>
        /// 添加用户角色
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public async Task<bool> InsertUserRoleInfo(List<UserRoleInfoDto> user)
        {
            List<UserRoleInfo> userRoleInfo = new List<UserRoleInfo>();
            _Mapper.Map(user, userRoleInfo);
            var result = await _UserRoleInfoRepository.AsSugarClient().Insertable(userRoleInfo).ExecuteCommandAsync();
            return result==1;

        }


        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="func"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public async Task<bool> DeleteUserRoleInfo(Expression<Func<UserRoleInfo, bool>> func)
        {
            return await _UserRoleInfoRepository.DeleteAsync(func);
        }

        public void Begin()
        {
            _UserRoleInfoRepository.AsTenant().BeginTran();
        }

        public void Commit()
        {
            _UserRoleInfoRepository.AsTenant().CommitTran();
        }

        public void RollBack()
        {
            _UserRoleInfoRepository.AsTenant().RollbackTran();
        }
    }
}
